package com.bdqn.zhao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.zhao.pojo.SysUser;
import com.bdqn.zhao.service.SysUserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/login")
public class LoginUserController {

    @Resource
    SysUserService sysUserService;


    @GetMapping("/tologin")
    public String tologin(){
        return "";
    }

    @GetMapping("/doLogin")
    public Object login(@RequestParam(value = "name",required = false) String name,
                        @RequestParam(value = "password",required = false) String password,
                        HttpSession session){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",name);
        SysUser sysUser =sysUserService.getOne(queryWrapper);
        if (sysUser!=null){
            queryWrapper.eq("password",password);
            SysUser sysUser1 =sysUserService.getOne(queryWrapper);
            if (sysUser1!=null){
                session.setAttribute("password",password);
                session.setAttribute("name",name);

                return "";
            }
            return "";
        }
        System.out.println(111);
        return "";

    }
    @PostMapping("/add")
    public String add(String name,String password){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",name);
        SysUser sysUser=sysUserService.getOne(queryWrapper);
        if (sysUser!=null){
            return "用户名重复";
        }
        SysUser sysUser1 = new SysUser();
        sysUser1.setLoginName(name);
        sysUser1.setPassword(password);
        if (sysUserService.save(sysUser1)){
            return "注册成功";
        }
        return "注册失败";
    }


}
